<!DOCTYPE HTML>
<html>

<head>
    <title> 倒计时 </title>
    <meta charset="utf-8">
    <!-- <script type="text/javascript">
        window.onload = function(){
            var m = 9;  //分
            var s = 59;  //秒
            var countdown = document.getElementById("title");
            getCountdown();
            setInterval(function(){ getCountdown() },1000);
            function getCountdown (){
                countdown.innerHTML = "<span>"+m+"</span> ：<span>"+s+"</span>";
                if( m == 0 && s == 0 ){
                    alert("倒计时结束");
                    m = 9;
                    s = 59;
                }else if( m >= 0 ){
                    if( s > 0 ){
                        s--;
                    }else if( s == 0 ){
                        m--;
                        s = 59;
                    }
                }
            }
        }
    </script> -->
</head>

<body>
    <h1>倒计时</h1>
    <div id="countdownBox"></div>
    <script>
        var countdown = {
            maxtime: 0,
            el: 0,
            timer: null,
            exec() {
                var maxtime = this.maxtime
                if (maxtime >= 0) {
                    var minutes = Math.floor(maxtime / 60)
                    var seconds = Math.floor(maxtime % 60)
                    var msg = "距离结束还有" + minutes + "分" + seconds + "秒"
                    this.el.innerHTML = msg
                    --this.maxtime
                } else {
                    clearInterval(this.timer)
                }
            },
            start(time, el) {
                var that = this
                that.maxtime = time
                that.el = el
                that.timer = setInterval(function () {
                    that.exec()
                }, 1000)
            },
            close() {
                clearInterval(this.timer)
            }
        }
        countdown.start(10 * 60, countdownBox)
    </script>


</body>

</html>